<?php
namespace app\admin\controller;
use think\Controller;
use think\Request;
class Myfile extends Controller
{
	public function ideOl(){
		$this->assign([
			'folder_path' => APP_PATH."index"
		]);
		return $this->fetch();
	}

	public function getMyFiles(){
		$data = $this->scanFile(APP_PATH."index");
		return ['code'=>1,'msg'=>'','data'=>$data];
	}

	/**
	 * [扫描文件]
	 */
	public function scanFile($path,$arr = []) {
		$files = scandir($path);
		unset($files[0]);	// . 同级
		unset($files[1]);	// ..上级
		foreach ($files as $file) {
			if (is_dir($path . '/' . $file)) {
				array_push($arr,['name'=>$file,'type'=>'folder','children'=>$this->scanFile($path . '/' . $file,[]),'path'=>$path."/".$file]);
			} else {
				array_push($arr,['name'=>basename($file),'type'=>'file','children'=>[],'path'=>$path."/".$file]);
			}
		}
		return $arr;
	}
	/**
	 * [获取文件内容]
	 */
	public function getFileConText(){
		if($this->request->IsPost()){
			$path = input('post.path');
			return ['code'=>1,'data'=>file_get_contents($path)];
		}
	}
	/**
	 * [保存代码]
	 */
	public function saveCode(){
		$param = input('post.');
		try{
			$file = fopen($param['path'],'w');
			fwrite($file, $param['code']);
			fclose($file);
		}catch(\Exception $e){
			return ['code'=>0,'msg'=>'保存失败:'.$e->getMessage()];
		}
		return ['code'=>1,'msg'=>'保存成功'];
	}


	/**
	 * [新建文件夹]
	 */
	public function NewFolder(){
		$param = input('post.');
		if($param['name'] == ''){
			return ['code'=>0,'msg'=>'请输入文件夹名称'];
		}
		$path = $param['path']."/".$param['name'];
		try{
			// $dir = iconv("UTF-8", "GBK", $path);
			mkdir ($path,0777,true);
			return ['code'=>1,'msg'=>'创建成功'];
		}catch(\Exception $e){
			return ['code'=>0,'msg'=>'创建失败:'.$e->getMessage()];
		}
	}

	/**
	 * [新建文件]
	 */
	public function NewFile(){
		$param = input('post.');
		if($param['name'] == ''){
			return ['code'=>0,'msg'=>'请输入文件名称'];
		}
		$path = $param['path']."/".$param['name'];
		try{
			$myfile = fopen($path, "w");
			fclose($myfile);
			return ['code'=>1,'msg'=>'创建成功'];
		}catch(\Exception $e){
			return ['code'=>0,'msg'=>'创建失败:'.$e->getMessage()];
		}
	}

	/**
	 * [删除文件夹]
	 */
	public function DeleteFolder(){
		$path = input('post.path');
		$result = $this->deleteAllFF($path);
		if($result['code'] == 1){
			try{
				rmdir($path);
			}catch(\Exception $e){
				return ['code'=>0,'msg'=>'删除失败:'.$e->getMessage()];
			}
			return ['code'=>1,'msg'=>'删除成功'];
		}else{
			return $result;
		}
	}

	public function deleteAllFF($path){
		$files = scandir($path);
		unset($files[0]);	// . 同级
		unset($files[1]);	// ..上级
		try{
			foreach ($files as $file) {
				if (is_dir($path . '/' . $file)) {
					$this->deleteAllFF($path . '/' . $file);
					rmdir($path . '/' . $file);
				} else {
					unlink($path . '/' . $file);
				}
			}
		}catch(\Exception $e){
			return ['code'=>0,'msg'=>'删除失败:'.$e->getMessage()];
		}
		return ['code'=>1,'msg'=>'删除成功'];
	}

	/**
	 * [删除文件]
	 */
	public function DeleteFile(){
		$path = input('post.path');
		try{
			unlink($path);
		}catch(\Exception $e){
			return ['code'=>0,'msg'=>'删除失败:'.$e->getMessage()];
		}
		return ['code'=>1,'msg'=>'删除成功'];
	}

	/**
	 * [重命名文件夹]
	 */
	public function RenameFolder(){
		$param = input('post.');
		if($param['name'] == ''){
			return ['code'=>0,'msg'=>'请输入名称'];
		}
		try{
			$oldName = $param['path'];
			$arr = explode("/",$param['path']);
			$arr[sizeof($arr)-1] = $param['name'];
			$newName = '';
			$arr = array_merge(array_filter($arr));
			foreach($arr as $v){
				$newName = $newName ."/". $v;
			}
			rename($oldName,$newName);
		}catch(\Exception $e){
			return ['code'=>0,'msg'=>'重命名失败：'.$e->getMessage()];
		}
		return ['code'=>1,'msg'=>'重命名成功'];
	}

	/**
	 * [前台首页]
	 */
	public function userFetch(){
		return $this->fetch(APP_PATH."index/view/index/index.html");
	}
}
